import { mock } from "mockjs";
import { CategorySign } from "src/models/categorySign";
import { DataList } from "src/business/models/user";

export let categorySignData: CategorySign[] = [
    {
        id: "1",
        handle_code: "品类标识1",
        handle_name: "品类类名1",
        handle_type: "类型1",
        user_id: "1",
        rc_code: "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=3630050586,4001820935&fm=58",
        create_date: "2018-03-21",
        modify_date: "2018-03-21",
        product_type: "产品类型1",
        custom_attribute: [{
            format: "foramt1",
            label: "属性1",
            value: "值1"
        }],
        company_handle_name: "公司1",
        company_handle_code: "公司标识码1",
        category_picture: ["https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=1871273354,3882757353&fm=58"],
    },
    {
        id: "2",
        handle_code: "品类标识2",
        handle_name: "品类类名2",
        handle_type: "类型2",
        user_id: "2",
        rc_code: "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=3630050586,4001820935&fm=58",
        create_date: "2019-03-01",
        modify_date: "2019-03-01",
        product_type: "产品类型2",
        custom_attribute: [{
            format: "foramt2",
            label: "属性2",
            value: "值2"
        }],
        company_handle_name: "公司2",
        company_handle_code: "公司标识码2",
        category_picture: ["https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=1871273354,3882757353&fm=58"],
    },
];

let single_data_for_category: any[] = [{
    id: '1',
    handle_code: '345',
    handle_name: '面粉1',
    handle_type: '食品',
    user_id: '222',
    rc_code: 'sadas',
    create_date: '20190318',
    modify_date: '20190317',
    product_type: '食品',
    custom_attribute: [],
    product_picture: 'd',
    company_handle_name: '行星系',
    company_handle_code: '8829',
    category_handle_name: '什么鬼',
    category_handle_code: 'cc',
}, {
    id: '2',
    handle_code: '345',
    handle_name: '面粉1',
    handle_type: '食品',
    user_id: '222',
    rc_code: 'sadas',
    create_date: '20190318',
    modify_date: '20190317',
    product_type: '食品',
    custom_attribute: [],
    product_picture: 'd',
    company_handle_name: '行星系',
    company_handle_code: '8829',
    category_handle_name: '什么鬼',
    category_handle_code: 'cc',
}, {
    id: '3',
    handle_code: '345',
    handle_name: '面粉1',
    handle_type: '食品',
    user_id: '222',
    rc_code: 'sadas',
    create_date: '20190318',
    modify_date: '20190317',
    product_type: '食品',
    custom_attribute: [],
    product_picture: 'd',
    company_handle_name: '行星系',
    company_handle_code: '8829',
    category_handle_name: '什么鬼',
    category_handle_code: 'cc',
}];

export function select_catagory(info: CategorySign, page_num?: number, size?: number) {
    let tmp: CategorySign[] = [];
    for (const key in info) {
        if (info.hasOwnProperty(key)) {
            categorySignData.map((e, i) =>
                e[key] === info[key] ? tmp.push(e) : null
            );
        }
    }

    let datalist: DataList<CategorySign> = {
        result: categorySignData,
        total: categorySignData.length
    };
    return mock({ result: datalist });
}

export function delete_catagory(ids: { id: string }[]) {
    for (const key in ids) {
        if (ids.hasOwnProperty(key)) {
            const element = ids[key];
            let index: number;
            categorySignData.find((e, i) => {
                if (e.id === element.id) {
                    index = i;
                    return true;
                }
                return false;
            });
            categorySignData.splice(index!, 1);
        }
    }
    return mock({ result: categorySignData });
}

export function get_category(id: string) {
    let index: number;
    categorySignData.find((e, i) => {
        if (e.id === id) {
            index = i;
            return true;
        }
        return false;
    });

    return mock({ result: categorySignData[index!] });
}

export function get_single_list_for_category(object?: Object, page_num?: number, num?: number) {
    let dataObject: any = {
        result: single_data_for_category,
        number: 3,
        msg_code: '0'
    };

    return mock({ result: dataObject });
}

export function get_single_count_for_category(object?: Object) {
    return mock({ result: single_data_for_category.length });
}